unit PingPongAction;

interface

uses Actors, SysUtils, Variants, Windows;

type
  TPingPong = class(TActor)
  public
    procedure Ping(var actData: Variant);
    procedure Pong(var actData: Variant);
  end;

  TPong = class;

  TPing = class(TActor)
  private
    FPong: TPong;
  public
    procedure Ping(var actData: Variant);
    property Pong: TPong read FPong write FPong;
  end;

  TPong = class(TActor)
  private
    FPing: TPing;
  public
    procedure Pong(var actData: Variant);
    property Ping: TPing read FPing write FPing;
  end;

implementation

procedure TPingPong.Ping(var actData: Variant);
var
  c,t:Integer;
begin
  // 0:Current, 1:Total, 2:ShowText, 3:StartTime,
  if actData[0] > 0 then begin
    if actData[2] then
      Writeln('Ping(', actData[0], ')');
    Send(Pong, actData);
  end else begin
    c := actData[1];
    t := GetTickCount-actData[3]+1;
    Writeln(Format('PingPong(%d): %d (%d Rounds/sec)', [c,t, (c*1000 div t)]));
  end;
end;

procedure TPingPong.Pong(var actData: Variant);
begin
  // 0:Current, 1:Total, 2:ShowText, 3:StartTime,
  if actData[2] then
    Writeln('Pong(', actData[0], ')');
  actData[0] := actData[0]-1;
  Send(Ping, actData);
end;

{ TPing }

procedure TPing.Ping(var actData: Variant);
var
  c,t:Integer;
begin
  // 0:Current, 1:Total, 2:ShowText, 3:StartTime,
  if actData[0] > 0 then begin
    if actData[2] then
      Writeln('Ping(', actData[0], ')');
    Send(Pong, Pong.Pong, actData);
  end else begin
    c := actData[1];
    t := GetTickCount-actData[3]+1;
    Writeln(Format('PingPong(%d): %d (%d Rounds/sec)', [c,t, (c*1000 div t)]));
  end;
end;

{ TPong }

procedure TPong.Pong(var actData: Variant);
begin
  // 0:Current, 1:Total, 2:ShowText, 3:StartTime,
  if actData[2] then
    Writeln('Pong(', actData[0], ')');
  actData[0] := actData[0]-1;
  Send(Ping, Ping.Ping, actData);
end;

end.